﻿var lichthidau = function ($) {
    var settings = {
        duration: 59000,
        requestUrl: "/Services/News/GetLichThiDau.ashx",
        table: '#tblLichThiDau'
    };

    var lichthidauInterval;
    function loadData(callback) {

        $.ajax({
            url: settings.requestUrl,
            dataType: 'json',
            error: function (ex) {
                clearInterval(lichthidauInterval);
                $(settings.table).hide();
            },
            success: function (ajaxResponse) {
                if (ajaxResponse.Success) {
                    bindData(ajaxResponse.Data);
                    execCallback(callback);
                }
            }
        });
    }

    function bindData(json) {
        if (json && json.length > 0) {
            var html = '';
            var format = '<td>{0}</td>';
            for (var i = 0; i < json.length; i++) {
                var session = json[i];
                html += formatString('<tr><th colspan="11" class="session-title">{0}</th></tr>', session.Name);
                var bet = session.Bet;
                if (bet && bet.length > 0) {
                    for (var j = 0; j < bet.length; j++) {
                        html += formatString('<tr class="{0}">', j % 2 == 0 ? 'even' : 'odd');
                        var betItem = bet[j];
                        html += formatString(format, betItem.MatchTime);
                        if (betItem.TopTeamId != 0) {

                            if (betItem.HomeTeamId == betItem.TopTeamId)
                                betItem.HomeTeamName = formatString("<span class='first'>{0}</span>", betItem.HomeTeamName);
                            if (betItem.VisitTeamId == betItem.TopTeamId)
                                betItem.VisitTeamName = formatString('<span class="first">{0}</span>', betItem.VisitTeamName);
                        }
                        html += formatString('<td>{0}<span>&nbsp;-&nbsp;</span>{1}</td>', betItem.HomeTeamName, betItem.VisitTeamName);
                        html += formatString(format, betItem.AsiaHomePrice);
                        html += formatString(format, betItem.AsiaContract);
                        html += formatString(format, betItem.AsiaVisitPrice);

                        html += formatString(format, betItem.OverPrice);
                        html += formatString(format, betItem.OverUnderContract);
                        html += formatString(format, betItem.UnderPrice);

                        html += formatString(format, betItem.EuropeHomePrice);
                        html += formatString(format, betItem.EuropeContract);
                        html += formatString(format, betItem.EuropeVisitPrice);
                        html += '</tr>';
                    }
                }

            }
            $(settings.table).find('tr:not(:first)').remove();
            $(settings.table).find("tr:first").after(html);
            $(settings.table).find("tr.even").css('background-color', '#ededed');
        }
    }

    function formatString(text) {
        if (arguments.length <= 1) {
            return text;
        }
        var tokenCount = arguments.length - 2;
        for (var token = 0; token <= tokenCount; token++) {
            text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
        }
        return text;
    }

    function execCallback(callback) {
        if (typeof callback == "function") {
            callback();
        }
    }

    function init(callback) {
        loadData(
                function () {
                    $(settings.table).show();
                    execCallback(callback);
                }
            );
        lichthidauInterval = setInterval(function () {
            loadData(callback);
        }, settings.duration);
    }

    return {
        init: function (callback) {
            return init(callback);
        }
    };
} (jQuery);